/**
 * 
 */
package org.mhz.sit.util;

import javafx.collections.ObservableList;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import org.apache.commons.collections.CollectionUtils;
import org.mhz.sit.entity.Speaker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Mahan
 *
 * Validation utility class
 */
public final class ValidationUtil
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ValidationUtil.class);
    
    /**
     * Private constructor
     */
    private ValidationUtil()
    {
        
    }
    
    /**
     * Is a number
     *
     * @param aValue - The value to check
     * @return If the value is a number
     */
    public static boolean isNumber(final String aValue)
    {
        // Value must be a digit that occurs one or more times
        if (!aValue.matches("\\d+?"))
        {
            return false;
        }        
        
        return true;
    }
    
    /**
     * Text field must contain a number
     *
     * @param aValue - The value to check
     * @param aField - The field containing the value
     */
    public static void textFieldMustContainANumber(final String aValue, 
                                                   final TextField aField)
    {
        if (!isNumber(aValue))
        {
            setStyleToTextField(aField, Constants.FIELD_ERROR_CLASS);
            aField.requestFocus();
            
            AlertUtil.displayAlert(Constants.SIT_001_INVALID_VALUE_ENTERED);            
        }
        else
        {
            setStyleToTextField(aField, Constants.FIELD_CLASS);
        }
    }
    
    
    /**
     * Is at least two characters
     *
     * @param aValue - The value to check
     * @return If the value is at least two characters
     */
    public static boolean isAtLeastTwoCharacters(final String aValue)
    {
        // Value must be a digit that occurs one or more times
        if (!aValue.matches(".{2,}"))
        {
            return false;
        }        
        
        return true;
    }    
    
    /**
     * Text field is at least two characters
     *
     * @param aValue - The value to check
     * @param aField - The field containing the value
     */
    public static void textFieldIsAtLeastTwoCharacters(final String aValue,
                                                       final TextField aField)
    {
        if (!isAtLeastTwoCharacters(aValue))
        {
            setStyleToTextField(aField, Constants.FIELD_ERROR_CLASS);
            aField.requestFocus();
            
            AlertUtil.displayAlert(Constants.SIT_002_AT_LEAST_TWO_CHARS); 
        }
        else
        {
            setStyleToTextField(aField, Constants.FIELD_CLASS);
        }
    } 
    
    /**
     * Check for duplicate speakers
     *
     * @param aValue - The value to check
     * @param aList - The list to check in
     * @param aField - The field to focus on
     */
    public static void checkDuplicateSpeaker(final Integer aValue, 
                                             final ObservableList<Speaker> aList,
                                             final TextField aField)
    {
        if (CollectionUtils.isNotEmpty(aList))
        {
            for (Speaker speaker : aList)
            {
                if ((speaker != null) && (speaker.getId().equals(aValue)))
                {
                    setStyleToTextField(aField, Constants.FIELD_ERROR_CLASS);
                    aField.requestFocus();  
                    
                    AlertUtil.displayAlert(Constants.SIT_003_VALUE_ALREADY_EXISTS); 
                }
            
            }                        
        }
        else
        {
            setStyleToTextField(aField, Constants.FIELD_CLASS);
        }        
    }
    
    /**
     * Set style to text field
     *
     * @param aField - The field to set
     * @param aStyle - The style to set
     */
    public static void setStyleToTextField(final TextField aField, 
                                           final String aStyle)
    {
        // Remove the other styles before adding
        aField.getStyleClass().remove(Constants.FIELD_ERROR_CLASS);
        aField.getStyleClass().remove(Constants.FIELD_CLASS);
        
        // Add the style to set
        aField.getStyleClass().add(aStyle);
    }
    
    /**
     * Set time style to a label
     *
     * @param aLabel - The label to set
     * @param aStyle - The style to set
     */
    public static void setTimeStyleToLabel(final Label aLabel, 
                                           final String aStyle)
    {
        // Remove the other styles before adding
        aLabel.getStyleClass().remove(Constants.NORMAL_TIME);
        aLabel.getStyleClass().remove(Constants.WARNING);
        aLabel.getStyleClass().remove(Constants.OVER_TIME);
        
        // Add the style to set
        aLabel.getStyleClass().add(aStyle);
    }    
}
